package com.tedu.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tedu.mappers.ItemCatMapper;
import com.tedu.pojo.ItemCat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Author：毕龙
 * Date: 2022/9/20 9:15
 */
@Service
public class ItemCatServiceImpl implements ItemCatService{

    @Autowired
    private ItemCatMapper itemCatMapper;

    /**
     * Map<Integer,list<ItemCat>>
     * map集合： key不可以重复  value是可以重复
     *      0,  一级菜单集合
     *  一级的id  二级菜单集合
     *  二级的id  三级菜单集合
     *业务思路分析：
     *  是否存在parentId
     *  不存在  我是第一个元素 封装到list集合中 之后保存在map
     *  存在    获取list 将其添加到其中
     *
     *
     *
     */

    public Map<Integer,List<ItemCat>> getMap(){
        //查询数据库
        List<ItemCat> list = itemCatMapper.selectList(null);
        //将list装在map
        HashMap<Integer, List<ItemCat>> map = new HashMap<>();
        for (ItemCat itemCat : list) {
            Integer parentId = itemCat.getParentId();
            if(map.containsKey(parentId)){
                map.get(parentId).add(itemCat);//
            }else{
                ArrayList<ItemCat> itemCats = new ArrayList<>();
                itemCats.add(itemCat);
                map.put(parentId, itemCats);
            }
        }

        return  map;
    }

    /**
     *
     * @param level
     * @return
     */
    @Override
    public List<ItemCat> findItemCatList(Integer level) {
        Map<Integer, List<ItemCat>> map = getMap();
        if(level == 1){
            return map.get(0);
        }
        if(level == 2){
            return getTwoList(map);
        }
        List<ItemCat> list = getThreeList(map);
        return list;
    }

    @Override
    @Transactional
    public void updateStatus(ItemCat itemCat) {
        //itemCat.setUpdated(new Date());
        itemCatMapper.updateById(itemCat);
    }

    @Override
    public void saveItemCat(ItemCat itemCat) {
        itemCat.setStatus(true);
        //自动回填充时间
        itemCatMapper.insert(itemCat);
    }

    @Override
    @Transactional
    public void updateItemCat(ItemCat itemCat) {
        itemCatMapper.updateById(itemCat);
    }

    @Override
    @Transactional
    public void deleteItemCat(ItemCat itemCat) {
        //1.删除3级
        if(itemCat.getLevel() == 3){
            itemCatMapper.deleteById(itemCat.getId());
        }

        //2.删除2级
        /**
         * SELECT * FROM item_cat WHERE NAME = '张姓'
         *
         * SELECT * FROM item_cat WHERE parent_id = 1184
         *
         * DELETE FROM item_cat WHERE id = 1184 OR parent_id = 1184
         */
        if(itemCat.getLevel() == 2){
            Integer parentId = itemCat.getId();
            //创建对象
            QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("parent_id", parentId)
                    .or()
                    .eq("id", parentId);

            itemCatMapper.delete(queryWrapper);
        }

        /**
         * 3.删除1级菜单 核心是二级菜单
         */
        if(itemCat.getLevel() == 1){
            //表示2级父类菜单的id
            Integer parentId_2 = itemCat.getId();
            //创建对象
            QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("parent_id", parentId_2);
            //获取二级菜单的ID
            List listID_2 = itemCatMapper.selectObjs(queryWrapper);
            queryWrapper.clear();
            queryWrapper.in(listID_2!=null && listID_2.size() >0, "parent_id",listID_2)
                    .or()
                    //.in(listID_2!=null && listID_2.size() >0,"id",listID_2);
                    .in("parent_id",parentId_2)
                    .or()
                    .in("id", parentId_2);

            itemCatMapper.delete(queryWrapper);


        }


    }

    private List<ItemCat> getThreeList(Map<Integer, List<ItemCat>> map){

        //获取一级和二级
        List<ItemCat> twoList = getTwoList(map);

        //编辑一级获取二级数据
        for (ItemCat itemCat : twoList) {
            List<ItemCat> children = itemCat.getChildren();
            if(children == null || children.size() == 0){
                continue;
            }
            for (ItemCat child : children) {
                Integer id = child.getId();
                List<ItemCat> itemCats = map.get(id);
                child.setChildren(itemCats);
            }
        }
        return twoList;

    }

    private List<ItemCat> getTwoList(Map<Integer, List<ItemCat>> map){
        //获取一级菜单
        List<ItemCat> itemCats = map.get(0);
        for (ItemCat itemCat : itemCats) {
            Integer id = itemCat.getId();
            List<ItemCat> itemCats1 = map.get(id);
            itemCat.setChildren(itemCats1);
        }
        return itemCats;//1级和二级
    }
}
